﻿using Nop.Core.Data;
using Nop.Core.Domain.Messages;
using Nop.Core.Events;
using Nop.Core.Plugins;
using Nop.Plugin.Misc.TabManager.Data;
using Nop.Plugin.Misc.TabManager.Domain;
using Nop.Services.Configuration;
using Nop.Services.Events;
using Nop.Web.Framework.Menu;
using Nop.Web.Framework.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nop.Services.Localization;

namespace Nop.Plugin.Misc.TabManager
{
    public class TabManagerPlugin : BasePlugin, IAdminMenuPlugin, IConsumer<EntityDeleted<NewsLetterSubscription>>
    {
        private TabManagerRecordObjectContext _context;
        private IRepository<Tab> _tabRepo;
        private ISettingService _settings;

        public TabManagerPlugin(TabManagerRecordObjectContext context, IRepository<Tab> tabRepo, ISettingService commonSettings)
        {
            _context = context;
            _tabRepo = tabRepo;
            _settings = commonSettings;
        }

        public bool Authenticate()
        {
            return true;
        }

        public override void Install()
        {
            _context.Install();
            _settings.SetSetting<bool>("TabManager.CacheMenuItems", false);
            _settings.SetSetting<bool>("TabManager.IncludeTopics", false);
            _settings.SetSetting<bool>("TabManager.IncludeCategories", false);
            _settings.SetSetting<bool>("TabManager.TrackChangeHistory", false);
            _settings.SetSetting<bool>("TabManager.UseKendoUIMenu", false);
            _settings.SetSetting<string>("TabManager.MainMenuClass", "top-menu");
            _settings.SetSetting<string>("TabManager.SubMenuContainerClass", "top-menu-triangle");
            _settings.SetSetting<string>("TabManager.SubMenuClass", "sublist firstLevel");
            _settings.SetSetting<string>("TabManager.MenuItemClass", "");

            this.AddOrUpdatePluginLocaleResource("TabManager.CacheMenuItems", "Cache Menu Items");
            this.AddOrUpdatePluginLocaleResource("TabManager.IncludeTopics", "Include Topics");
            this.AddOrUpdatePluginLocaleResource("TabManager.IncludeCategories", "Include Categories");
            this.AddOrUpdatePluginLocaleResource("TabManager.TrackChangeHistory", "Track Change History");
            this.AddOrUpdatePluginLocaleResource("TabManager.UseKendoUIMenu", "Use KendoUI Menu");
            this.AddOrUpdatePluginLocaleResource("TabManager.MainMenuClass", "Main Menu Class");
            this.AddOrUpdatePluginLocaleResource("TabManager.SubMenuContainerClass", "Sub Menu Container Class");
            this.AddOrUpdatePluginLocaleResource("TabManager.SubMenuClass", "Sub Menu Class");
            this.AddOrUpdatePluginLocaleResource("TabManager.MenuItemClass", "Menu Item Class");
            base.Install();
        }

        public override void Uninstall()
        {
            _context.Uninstall();

            this.DeletePluginLocaleResource("TabManager.CacheMenuItems");
            this.DeletePluginLocaleResource("TabManager.IncludeTopics");
            this.DeletePluginLocaleResource("TabManager.IncludeCategories");
            this.DeletePluginLocaleResource("TabManager.TrackChangeHistory");
            this.DeletePluginLocaleResource("TabManager.UseKendoUIMenu");
            this.DeletePluginLocaleResource("TabManager.MainMenuClass");
            this.DeletePluginLocaleResource("TabManager.SubMenuContainerClass");
            this.DeletePluginLocaleResource("TabManager.SubMenuClass");
            this.DeletePluginLocaleResource("TabManager.MenuItemClass");

            var settings = _settings.GetAllSettings();
            if (settings != null && settings.Count > 0)
            {
                //this is a database delete
                foreach (var stt in settings)
                {
                    if (stt.Name.Equals("TabManager.CacheMenuItems"))
                        _settings.DeleteSetting(stt);

                    if (stt.Name.Equals("TabManager.IncludeTopics"))
                        _settings.DeleteSetting(stt);

                    if (stt.Name.Equals("TabManager.IncludeCategories"))
                        _settings.DeleteSetting(stt);

                    if (stt.Name.Equals("TabManager.TrackChangeHistory"))
                        _settings.DeleteSetting(stt);

                    if (stt.Name.Equals("TabManager.UseKendoUIMenu"))
                        _settings.DeleteSetting(stt);

                    if (stt.Name.Equals("TabManager.SubMenuClass"))
                        _settings.DeleteSetting(stt);

                    if (stt.Name.Equals("TabManager.SubMenuContainerClass"))
                        _settings.DeleteSetting(stt);

                    if (stt.Name.Equals("TabManager.SubMenuClass"))
                        _settings.DeleteSetting(stt);

                    if (stt.Name.Equals("TabManager.MenuItemClass"))
                        _settings.DeleteSetting(stt);
                }
            }
            settings = null;


            base.Uninstall();
        }

        public Web.Framework.Menu.SiteMapNode BuildMenuItem()
        {
            return new SiteMapNode { Visible = true, Title = "Tab Manager", Url = "/TabManager/ManageTabs" };
        }


        public void HandleEvent(EntityDeleted<NewsLetterSubscription> eventMessage)
        {
            //Tab entity = _trialRepo.Table.Where(x => x.CustomerEmail == eventMessage.Entity.Email).FirstOrDefault();
            //entity.OnMailingList = false;
            //_trialRepo.Update(entity);
        }

    }
}
